<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Snap.svg操作SVG</title>
    <style>
      body,
      ul {
        margin: 0;
        padding: 0;
      }

      body {
        background-image: url(../images/grid.png);
      }
      svg {
        background-color: rgba(255, 0, 0, 0.1);
      }
    </style>
  </head>
  <body>
    <svg
      id="snap-svg"
      width="300"
      height="300"
      xmlns="http://www.w3.org/2000/svg"
    >
      <rect id="rectangle1" x="0" y="0" width="100" height="50"></rect>
    </svg>
    <script src="./libs/snap.svg-min.js"></script>
    <script>
      window.onload = function () {
        const svg = Snap("#snap-svg");
        const paper = svg.paper; // 获取svg画布

        const rectangle = paper.rect(0, 100, 100, 50);
        rectangle.attr({
          fill: "#ff0000",
        });

        const rectangle1 = paper.select("#rectangle1"); // 获取svg中的元素
        rectangle1.attr({
          fill: "#00ff00",
          stroke: "#000000",
          strokeWidth: 2,
        });
      };
    </script>
  </body>
</html>
